package com.zyl.blog.common.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.zyl.blog.common.service.ITreeNode;
import lombok.Data;

import java.util.List;

/**
 * @version v1.0
 * @ProjectName: Personal_Blog_Server
 * @Package: com.zyl.blog.common.model
 * @ClassName: TreeModel
 * @Description: 树形结构模型抽象父类
 * @param <T> ID类型
 * @Author FinalFantasy
 * @Date 2025/9/2-13:44
 */
@Data
public abstract class TreeModel<T> implements ITreeNode<T> {

    /** 子节点集合 */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<ITreeNode<T>> children;

    /** 完整路径（存储所有父节点ID） */
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private String ancestors;

    /** 父节点ID */
    private T parentId;

    /**
     * 由子类实现，返回自身的ID
     * @return 实体ID
     */
    @JsonIgnore
    public abstract T getEntityId();

    /**
     * 实现ITreeNode接口的getId方法
     * 统一返回子类的实体ID
     */
    @Override
    public T getId() {
        return getEntityId();
    }

    @Override
    public T getParentId() {
        return parentId;
    }

    @Override
    public List<ITreeNode<T>> getChildren() {
        return children;
    }

    @Override
    public void setChildren(List<ITreeNode<T>> children) {
        this.children = children;
    }
}
